/**
 * 修改手机号
 */
var getyzm_able = true;
var localStorageManagers = require("script/localStorageManagers.js");
var downloadUtils = require('scripts/Core/DownLoadUtil.js');
var mobileFrame = require('scripts/Core/MobileFrame.js');
//mui初始化
mui.init({
	swipeBack: true
});
//plus初始化
mui.plusReady(plusReady);
//plusReady
function plusReady() {
	//获取验证码
	mui('body').on('tap', '#codebtn2', function() {
		if (getyzm_able == true) {
			var mobile = document.getElementById('newmobile').value;
			this.className = 'yb';
			timedCount2();
			sendCheckcode(mobile);
		}
	});
	//确定
	mui('body').on('tap', '.submit', saveMobile);
	//取消
	mui('body').on('tap', '.cancel', function(){
		mui.back();
	});
};
//发送验证码
function sendCheckcode(mobile) {
	var tips=isFormat(mobile);
	if(tips!=''&&tips.length>0){
		mui.toast(tips);return;
	}
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + 'mobile/user/getMobileTelCheckCode1.do';
	mui.ajax(url, {
		data: {
			userid: userid,
			mobile: mobile
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			//if(data==null||data.length==0)return;
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
function isFormat(mobile){
	var tips='';
if (mobile.length > 0) {
		var re = /(^1[3|5|8][0-9]{9}$)/;
		var tips = '';
		if (trim(mobile) != '') {
			if (!re.test(mobile)) {
				tips='请输入正确的手机号码';
			}
		}
	} else {
		tips="请输入手机号";
	}	
	return tips;
}
//计时
function timedCount2() {
	var interval = 1000;
	var len = 20000;
	if (codebtn2.value == null || codebtn2.value < 10 || codebtn2.value == len) {
		codebtn2.value = len;
		getyzm_able = false;
		codebtn2.innerHTML = (len / 1000) + "秒后重发";
	}
	var i = codebtn2.value - interval;
	if (i <= 100) {
		getyzm_able = true;
		codebtn2.value = len;
		codebtn2.innerHTML = "获取验证码";
		codebtn2.className = 'yb able';
		return;
	}
	codebtn2.value = i;
	codebtn2.innerHTML = (i / 1000) + "秒后重发";
	setTimeout("timedCount2()", interval);
}
//保存手机号
function saveMobile() {
	var mobile = document.getElementById('newmobile').value;
	var tips=isFormat(mobile);
	if(tips.length>0){
		mui.toast(tips);
		return;
	}
	var code = document.getElementById('code2').value;
	code = trim(code);
	if (code == '') {
		mui.toast('请输入验证码');
		return;
	}
	
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + 'mobile/user/updateMobile.do';
	mui.ajax(url, {
		data: {
			mobile:mobile,
			userid: userid,
			code: code
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data.code == 0) {
				mui.back();
				mui.toast('修改手机号成功');
			} else {
				mui.toast('验证码错误');
			}
		},
		error: function(e1, e2, e3) {
			console.log(e1);
		}
	});
}

function trim(str) {
	var strnew = str.replace(/^\s*|\s*$/g, "");
	return strnew;
}